(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.mgl.updateController", updateController);
	updateController.$inject = [ "$scope", "$q", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService", "mglService" ];
	function updateController($scope, $q, modalService, gridService, layoutConfig, validatorConfig, authService, mglService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.mgl = {};
		ctrl.mgl.mglentrydetails = [];
		ctrl.effectiveDateReadonly = false;

		ctrl.init = function() {
			if (ctrl.validatorConfig)
				ctrl.layoutConfig.formConfigs.mglDetail.tableConfigs.table.validatorConfig = ctrl.validatorConfig.mglentrydetails;
			ctrl.mglentrydetailsGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.mglDetail.tableConfigs.table, null);
			ctrl.mglentrydetailsGridOptions.data = "vm.mgl.mglentrydetails";

			mglService.initDataOptions().then(function(optionsData) {
				ctrl.glCcyOptipns = optionsData.currency;
				ctrl.txnPartyTypeOptions = optionsData.allocCode;
				authService.updateInit(params).then(function(response) {
					ctrl.mgl = response.data.model || {};
					ctrl.mgl.taskId = response.data.task.taskId;
					if (!ctrl.mgl.mglentrydetails) {
						ctrl.mgl.mglentrydetails = [];
					} else {
						angular.forEach(ctrl.mgl.mglentrydetails, function(row) {
							row.custCode = ctrl.mgl.custCode;
							row.baseCcy = ctrl.mgl.baseCcy;
						});
					}
				});

			});
		};

		ctrl.addRow = function(row) {
			return mglService.addRow(row, ctrl.mgl);
		};

		ctrl.glAmtChanged = function(row, form, flag) {
			mglService.glAmtChanged(row, flag, ctrl.mgl);
		};

		ctrl.glAmtBChanged = function(row, form, flag) {
			mglService.glAmtBChanged(row, flag, ctrl.mgl);
		};

		ctrl.fxRateChanged = function(row, form, flag) {
			mglService.fxRateChanged(row, flag, ctrl.mgl);
		};

		ctrl.acctCodechanged = function(row, form, firstLoad) {
			mglService.acctCodechanged(row, firstLoad, ctrl.mgl);
		};

		ctrl.glCcyChanged = function(row, form, flag) {
			mglService.glCcyChanged(row, flag, ctrl.mgl);
		};

		// button
		ctrl.save = function() {
			mglService.resetMglentrydetails(ctrl.mgl.mglentrydetails);
			return authService.updateSaved(ctrl.mgl).then(submitSuccessful, submitFailed);
		};

		ctrl.submit = function() {
			mglService.resetMglentrydetails(ctrl.mgl.mglentrydetails);
			return authService.updateSubmitted(ctrl.mgl).then(submitSuccessful, submitFailed);
		};

		ctrl.cancel = function() {
			modalService.openConfirmDialogs({
				"messages" : [ {
					msg : "title.dialog.confirm.cancel",
					args : [ ctrl.mgl.primaryKey ]
				} ],
				"btnCallback" : {
					"ok" : function() {
						mglService.resetMglentrydetails(ctrl.mgl.mglentrydetails);
						authService.updateCancelled(ctrl.mgl).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
			if (!ctrl.mgl.mglentrydetails) {
				ctrl.mgl.mglentrydetails = [];
			} else {
				angular.forEach(ctrl.mgl.mglentrydetails, function(row) {
					row.custCode = ctrl.mgl.custCode;
					row.baseCcy = ctrl.mgl.baseCcy;
					ctrl.acctCodechanged(row, undefined, true);
				});
			}
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.cancel" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}
	}
})(angular);
